package com.cf.studiomanage.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    private Boolean success;
    private Integer code;
    private String message;
    private Object data;

    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("成功");
        return r;
    }

    public static R ok(String msg) {
        R r = ok();
        r.setMessage(msg);
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(500);
        r.setMessage("失败");
        return r;
    }

    public static R error(String msg) {
        R r = error();
        r.setMessage(msg);
        return r;
    }

    public R data(String key, Object value) {
        if (!(this.data instanceof Map)) {
            this.data = new HashMap<String, Object>();
        }
        ((Map<String, Object>) this.data).put(key, value);
        return this;
    }


    public R data(Object value) {
        this.data = value;
        return this;
    }
}
